<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Menu extends Model
{
    //
    public function getMenus(){

        $menus =  $this->select('id','pid','name','permission_name' , 'sort','status','is_menu')
                    ->where('status',0)
                    ->orderBy('pid', 'asc')
                    ->orderBy('sort', 'asc')
                    ->get()->toArray();

        $returnMenu = [];

        foreach ($menus as $menu){
            $tempMenus = [];
            if($menu['is_menu'] == 1){

                foreach ($menus as $val){
                    if ($menu['id'] == $val['pid']){
                        $tempMenusData = [];

//                        $tempMenusData['isMenu'] = $val['is_menu'];
                        $tempMenusData['id'] = $val['id'];
                        $tempMenusData['name'] = $val['name'];
                        $tempMenusData['permissionName'] = $val['permission_name'];
                        $tempMenusData['sort'] = $val['sort'];

                        if(empty($val['permission_name'])){
                            $tempMenusData['permissionCreate'] = '0';
                            $tempMenusData['permissionShow'] = '0';
                            $tempMenusData['permissionEdit'] = '0';
                            $tempMenusData['permissionDestroy'] = '0';
                        }else{
//                            $val['permission'] = $this->getMenusPermission($val['permission_name']);
//                            $tempMenusData['permissionCreate'] = request()->user()->can("edit-".$val['permission_name'])?'1':'0';
//                            $tempMenusData['permissionShow'] = request()->user()->can("show-".$val['permission_name'])?'1':'0';
//                            $tempMenusData['permissionEdit'] = request()->user()->can("edit-".$val['permission_name'])?'1':'0';
//                            $tempMenusData['permissionDestroy'] = request()->user()->can("destroy-".$val['permission_name'])?'1':'0';

                            $tempMenusData['permissionCreate'] = '1';
                            $tempMenusData['permissionShow'] = '1';
                            $tempMenusData['permissionEdit'] = '1';
                            $tempMenusData['permissionDestroy'] = '1';

                        }

                        $tempMenus[] = $tempMenusData;
                    }
                }
                $down['id'] = $menu['id'];
                $down['name'] = $menu['name'];
                $down['pid'] = $menu['pid'];
                $down['permissionName'] = $menu['permission_name'];
                $down['downMenu'] = $tempMenus;
                $returnMenu[] = $down;
            }


            unset($tempMenus);
        }

        return $returnMenu;
    }

    public function getMenusPermission($permissionName){

        $data['create'] = request()->user()->can("edit-".$permissionName)?'1':'0';
        $data['show'] = request()->user()->can("show-".$permissionName)?'1':'0';
        $data['edit'] = request()->user()->can("edit-".$permissionName)?'1':'0';
        $data['destroy'] = request()->user()->can("destroy-".$permissionName)?'1':'0';

        return $data;
    }
}
